package com.shareach.http.match;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shareach.common.BizCondition;
import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.domain.BaseTelephoneDomain;
import com.shareach.domain.BizTuanShopDomain;
import com.shareach.domain.DiscountType;
import com.shareach.domain.common.Bind;
import com.shareach.service.BaseTelphoneService;
import com.shareach.service.BizTuanShopService;
import com.shareach.util.Levenshtein;

@Service
public class MatchShopUtil {
	@Autowired
	private BaseTelphoneService telphoneService;
	@Autowired
	private BizTuanShopService tuanShopService;
	@Autowired
	private MatchRDao matchDao;
	
	/**
	 * 移除地址后面的括号内容
	 * @param addr
	 * @return
	 */
	private String removeAddrEx(String addr){
		int idx = addr.indexOf("(");
		if(idx>0){
			return addr.substring(0,idx);
		}else{
			idx = addr.indexOf("（");
			if(idx>0){
				return addr.substring(0,idx);
			}
		}
		return addr;
	}
	
	/**
	 * 匹配商户信息
	 * @param city
	 * @param type
	 * @param discountShopId
	 * @param brandName
	 * @param name
	 * @param orgName
	 * @param addr
	 * @param tels
	 * @return
	 */
	public boolean matchShop(String city, DiscountType type, Long discountShopId, String brandName, String name, String orgName, String addr, String... tels){
		if(matchShopByTel(city, type, discountShopId, brandName, name, orgName, addr, tels)){
			return true;
		}else{
			return matchShopByName(city, type, discountShopId, brandName, name, orgName, addr);
		}
	}
	/**
	 * 根据名称匹配
	 * @param city
	 * @param type
	 * @param discountShopId
	 * @param brandName
	 * @param name
	 * @param orgName
	 * @param addr
	 * @return
	 */
	private boolean matchShopByName(String city, DiscountType type, Long discountShopId, String brandName, String name, String orgName, String addr){
		List<BaseTelephoneDomain> telDomains = telphoneService.getList(new BizCondition().put("bind.brandName", brandName).put("bind.name", name));
		if(telDomains==null || telDomains.isEmpty()) return false;
		for(BaseTelephoneDomain telDomain:telDomains ){
			for(Bind bindCurr : telDomain.getBind()){
				if(name.equalsIgnoreCase(bindCurr.getName())){
					if(StringUtils.isBlank(brandName)){
						saveMatchResult(type, telDomain.getShopId(), discountShopId, bindCurr);
						return true;
					}else if(brandName.equals(bindCurr.getBrandName())){
						saveMatchResult(type, telDomain.getShopId(), discountShopId, bindCurr);
						return true;
					}
				}
			}
		}
		return false;
	}
	/**
	 * 根据电话匹配
	 * @param city
	 * @param type
	 * @param discountShopId
	 * @param brandName
	 * @param name
	 * @param orgName
	 * @param addr
	 * @param tels
	 * @return
	 */
	private boolean matchShopByTel(String city, DiscountType type, Long discountShopId, String brandName, String name, String orgName, String addr, String... tels){
		Long shopId = null;
		Bind bind = null;
		float simular = 0;
		for(String tel:tels){
			//匹配电话
			BaseTelephoneDomain telDomain = telphoneService.getByTel(city, tel);
			if(telDomain==null) continue;
			for(Bind item:telDomain.getBind()){
				//匹配名称相似度
				if(StringUtils.isNotBlank(name)){
					if(StringUtils.isNotBlank(brandName)){//如果有品牌则先确定品牌名的
						if(Levenshtein.getSimilarityRatio(brandName, item.getBrandName())<0.6) continue;
					}
					//如果名称相似度足够则确定为可以匹配
					float simularCurr = Levenshtein.getSimilarityRatio(name, item.getName());
					if(simularCurr<0.7) continue;
					if(simularCurr>simular){
						simularCurr= simular;
						shopId = telDomain.getShopId();
						bind = item;
					}
				}
				if(StringUtils.isNotBlank(orgName)){
					float simularCurr = Levenshtein.getSimilarityRatio(orgName, item.getOrgName());
					if(simularCurr<0.7) continue;
					if(simularCurr>simular) {
						simularCurr= simular;
						shopId = telDomain.getShopId();
						bind = item;
					}
				}
				if(shopId!=null &&  StringUtils.isNotBlank(addr)){
					//地址相似度
					float simularCurr = Levenshtein.getSimilarityRatio(removeAddrEx(addr), (item.getAddr()));
					if(simularCurr<0.7) continue;
					if(simularCurr>simular) {
						simularCurr= simular;
						shopId = telDomain.getShopId();
						bind = item;
					}
				}
			}
		}
		if (simular>0.6){
			saveMatchResult(type, shopId, discountShopId, bind);
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 保存匹配
	 * @param type
	 * @param shopId
	 * @param discountShopId
	 * @param bind
	 */
	private void saveMatchResult(DiscountType type, Long shopId, Long discountShopId, Bind bind){
		if(shopId!=null){
			//保存数据
			switch(type){
				case Ticket://优惠券
					break;
				case Tuan://团购
					BizTuanShopDomain tuanShop = new BizTuanShopDomain();
					tuanShop.setShopId(shopId);
					//tuanShop.setSimular(simular)
					tuanShopService.update(tuanShop, new BizCondition().put("_id",discountShopId));
					matchDao.removeDiscountShopUnmap(type, discountShopId);
					break;
				case Activity://活动
					
					break;
				case Card://卡
					break;
				default:break;
			}
		}else{
			//需要下载商户信息,更新到下载商户列表队列
			matchDao.setSnatchShop(bind.getSite(), bind.getSiteId());
		}
	}
}
